package org.ocean.online.web.workspace;

import java.util.HashMap;
import java.util.Map;

import org.apache.wicket.markup.MarkupType;
import org.ocean.online.utils.DataLoader;
import org.ocean.online.web.Login;
import org.ocean.online.wicket.NeedLoginPage;

public class LoadJson extends NeedLoginPage{

	private static final long serialVersionUID = -1639928436712290677L;

	public LoadJson(){
		
	}

	@Override
	public MarkupType getMarkupType() {
		return new MarkupType("json", "application/json");
	}

	@Override
	public void renderPage() {
		String data = this.getPage().getPageParameters().get("parent").toString("");
		if(getUser()==null){
			this.getPage().redirectToInterceptPage(new Login());
		}
		String json="[]";
		String modelName = this.getPage().getRequest().getRequestParameters().getParameterValue("modelName").toString("");
		String projectName = this.getPage().getRequest().getRequestParameters().getParameterValue("projectName").toString("");
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("projectName", projectName);
		param.put("modelName", modelName);
		param.put("user", getUser());
		if("allprojects".equals(data)){
			json = DataLoader.loadProjects(getUser());
		}else if("project".equals(data)){
			json = DataLoader.loadCatagory();
		}else if("model".equals(data)){
//			json = DataLoader.loadModelsOfProject(session.getUser());
		}else if("loadmodel".equals(data)){
			json = DataLoader.loadModel(param);
		}
//		try {
//			json = FileUtils.readFileToString(new File(this.getClass().getResource("tree_data.json").toURI()));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (URISyntaxException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		this.getResponse().write(json);
	}
	
}
